<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\HomeUser;
use App\Http\Model\Goods;
use App\Http\Model\Type;
use App\Http\Model\Idea;
use App\Http\Model\Home_user;
use App\Http\Model\Order;
use App\Userinfo;

class UserController extends Controller
{   
    // 加载个人中心意见反馈页面
    public function idea(){
        $user = $this->pub();
        $link = $this->pub2();

       return view('home.user.idea',compact('user','link'));
    }
    // 意见反馈信息处理页
    public function doidea(Request $request){
        $data = $request->except('_token');
        // dd($data);
        $idea = new Idea;
       $idea->title = $data['title'];
       $idea->content = $data['content'];
       $idea->pic = $data['pic'];

         if($idea->save()){
            return redirect('/idea')->with('message', '意见反馈成功!我们收到后将酌情采纳您的宝贵意见!');
        }else{
            return back()->with('errors','添加失败');
        }
      
    }
    

    /**
     * 公共方法
     *
     * @return \Illuminate\Http\Response
     */
    public function pub(){
        $user = Home_user::find(session('huser')['id']);
        @$user['photo'] = \DB::table('sad_userinfo')->where('uid',session('huser')['id'])->get()[0]['photo'];
        return $user;
    }
    public function pub2(){
        $link = \DB::table('sad_link')->orderBy('order','asc')->get();
        return $link;
    }
    /**
     * 个人详情首页
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = $this->pub();
        $link = $this->pub2();
        // $posts = DB::table('posts')->paginate(3);
        $goods = \DB::table('sad_goods')->where('uid',$user['id'])->orderBy('ctime','desc')->paginate(6);
        //
        
       return view('home.user.index',compact('goods','user','link'));
    }

    /**
     * 我的购买
     *
     * @return \Illuminate\Http\Response
     */
    public function mybuy()
    {
        $user = $this->pub();
        // $posts = DB::table('posts')->paginate(3);
        $order = \DB::table('sad_order')->where('uid',$user['id'])->paginate(6);
        // dd($user['id']);
        //
        // dd($user['id']);
        $order = Home_user::find($user['id'])->home_user1;
        // dd($order);
            # code...
             foreach ($order as $key => $value) {
              $good =  \DB::table('sad_goods')->where('id',$value['gid'])->first();
              $value['title'] = $good['title'];
              $value['pic'] = $good['pic'];
              $value['status'] = $good['status'];
              $value['shipments'] = $good['shipments'];
              
         
        }
        
        $link = $this->pub2();
       return view('home.user.mybuy',compact('order','user','link'));
    }

    /**
     * 确认收货
     *
     * @return \Illuminate\Http\Response
     */
    public function affirm(Request $request)
    {
        $request = $request->all();
        $order = Order::find($request['id']);
        $good = Goods::find($order['gid']);
        $good -> status = '交易完成';
        $res = $good -> save();
        return back();
    }
    /**
     * 我的订单删除
     *
     * @return \Illuminate\Http\Response
     */
    public function myorderdel(Request $request)
    {
        $request = $request->all();
        $order = Order::find($request['id']);
        $order -> delete();
        return back();
    }


     /**
     * 发货页面
     *
     * @return \Illuminate\Http\Response
     */
    public function shipment(Request $request)
    {
         $user = $this->pub();
         $link = $this->pub2();
         $id = $request->all()['id'];
         $good = Goods::find($id);
        return view('home.user.shipment',compact('user','good','link'));
    }

    /**
     * 发货动作
     *
     * @return \Illuminate\Http\Response
     */
    public function doshipment(Request $request)
    {
         // dd($request->all());
         $wuliu = $request['wuliu'];
         $shipments = $request['shipments'];
         $shipments = $wuliu.'('.$shipments.')';
         $good = Goods::find($request['id']);
         $good->status = '已发货';
         $good->shipments = $shipments;
         $res = $good->save();
         // dd($res);
         return redirect('/user');
    }

    /**
     * 卖家查看订单
     *
     * @return \Illuminate\Http\Response
     */
    public function showorder(Request $request)
    {
        //公共方法
        $user = $this->pub();
        $link = $this->pub2();
        //订单
        $request = $request->all();
        $goods = Goods::find($request['id']);
        $order = \DB::table('sad_order')->where('gid',$request['id'])->first();
        return view('home.user.showorder',compact('user','link','goods','order'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * 用户详情
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show()
    {
        //
        $link = $this->pub2();
        $userinfo = \DB::table('sad_userinfo')->where('uid',session('huser')['id'])->get()[0];
        $user = $this->pub();
        // dd($userinfo);
        $goods = \DB::table('sad_goods')->where('uid',session('huser')['id'])->get();
        $goods = count($goods);
        $order = \DB::table('sad_order')->where('rid',session('huser')['id'])->get();
        $order = count($order);
        return view('home/user/show',compact('user','userinfo','goods','link','order'));
    }

    /**
     * 个人信息修改页面
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //

        $user = $this->pub();
        $link = $this->pub2();
        $userinfo = Userinfo::find($id);

        return view('home.user.edit',compact('userinfo','user','link'));
    }

    /**
     * 个人信息修改动作
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //

        $request = $request->all();
        // dd($request);
        $user['username'] = $request['username'];
        $user['phone'] = $request['phone'];
        $user['email'] = $request['email'];
        $userinfo['birthday'] = $request['birthday'];
        $userinfo['address'] = $request['address'];
        $userinfo['photo'] = $request['photo'];
        $userinfo['sex'] = $request['sex'];
        $userinfo['nickname'] = $request['nickname'];

        $huser = Home_user::find(session('huser')['id']);
        $res = $huser -> update($user);
        $res2 = \DB::table('sad_userinfo')->where('id',$id)->update($userinfo);
        return redirect('/user/'.session('huser')['id']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    //检查手机号是否存在
     public function getVerifyCode(Request $request)
    {
        $phone =  $request->input('phone');

        $res1 = Home_user::where('phone',$phone)->first();
       if($res1){
            return 1;
        }else{
            return 2;
        }
    }

    //获取手机验证码
     public function phoneCode(Request $request)
    {
        $data = $request->all();
       if($data['code']==session('phone')){
            return 1;
        }else{
            return 2;
        }
    }

    //检测用户名是否存在
      public function username(Request $request)
    {
        $data = $request->all();
        // dd($data['username']);
        $res = Home_user::where('username',$data['username'])->get();
        if ($res) {
            return 1;
        }else{
            return 2;
        }
    }
    //检测邮箱是否存在
      public function email(Request $request)
    {
        $data = $request->all();
        // dd($data['username']);
        $res = Home_user::where('email',$data['email'])->first();
        if ($res) {
            return 1;
        }else{
            return 2;
        }
    }

    //签到
     public function qiandao(Request $request)
    {
        $jifen = $request->all();
        $code = $jifen['code']+5;
        $user = Userinfo::find($jifen['id']);
        $user -> code =$code;
        $user -> codetime = time();
        $user->save();
        $res = Userinfo::find($jifen['id'])['codetime'];

        return $res ;
    }
    //签到
     public function jifen(Request $request)
    {
        $jifen = $request->all();
        // $code = $jifen['code']+5;
        // $user = Userinfo::find($jifen['id']);
        // $user -> code =$code;
        // $user -> codetime = time();
        // $user->save();
        $res = Userinfo::find($jifen['id'])['codetime'];

        return $res ;
    }


}
